package cn.wjx.taobaounion.ui.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.util.ArrayList;
import java.util.List;

import cn.wjx.taobaounion.R;
import cn.wjx.taobaounion.model.domain.SelectedCategories;

/**
 * @author WuChangJian
 * @date 2020/6/25 15:52
 */
public class SelectedPageLeftAdapter extends RecyclerView.Adapter<SelectedPageLeftAdapter.InnerHolder> {
    private List<SelectedCategories.DataBean > mDataBeans = new ArrayList<>();
    private int mCurrentSelectedPosition = 0;
    private OnItemClickListener mListener;

    @NonNull
    @Override
    public InnerHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_selected_left, parent, false);
        return new InnerHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull InnerHolder holder, int position) {

        TextView categoryTitle = holder.itemView.findViewById(R.id.left_category_tv);
        if (mCurrentSelectedPosition == position) {
            categoryTitle.setBackgroundColor(categoryTitle.getResources().getColor(R.color.colorEEEEEE, null));
        } else {
            categoryTitle.setBackgroundColor(categoryTitle.getResources().getColor(R.color.white, null));
        }
        SelectedCategories.DataBean dataBean = mDataBeans.get(position);
        categoryTitle.setText(dataBean.getFavorites_title());
        // 点击切换条目类别
        holder.itemView.setOnClickListener(v-> {
            if (mListener != null && mCurrentSelectedPosition != position) {
                mCurrentSelectedPosition = position;
                mListener.onLeftItemClick(dataBean);
                notifyDataSetChanged();
            }
        });
    }

    @Override
    public int getItemCount() {
        return mDataBeans.size();
    }

    public void setData(List<SelectedCategories.DataBean> data) {
        mDataBeans.clear();
        mDataBeans.addAll(data);
        notifyDataSetChanged();
        if (mListener != null) {
            mListener.onLeftItemClick(data.get(mCurrentSelectedPosition));
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mListener = listener;
    }
    public interface OnItemClickListener {
        void onLeftItemClick(SelectedCategories.DataBean dataBean);
    }

    public class InnerHolder extends RecyclerView.ViewHolder {
        public InnerHolder(@NonNull View itemView) {
            super(itemView);
        }
    }
}
